package com.zhourh.bang.modular.edu.controller;

import com.zhourh.bang.common.annotion.Permission;
import com.zhourh.bang.common.controller.BaseController;
import com.zhourh.bang.common.exception.BizExceptionEnum;
import com.zhourh.bang.common.exception.BussinessException;
import com.zhourh.bang.common.node.ZTreeNode;
import com.zhourh.bang.common.persistence.dao.SClazzMapper;
import com.zhourh.bang.common.persistence.model.SClazz;
import com.zhourh.bang.core.util.ToolUtil;
import com.zhourh.bang.modular.edu.dao.ClazzDao;
import com.zhourh.bang.modular.edu.service.IClazzService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;

/**
 * 班级控制器
 *
 * @author fengshuonan
 * @Date 2018-06-22 10:53:38
 */
@Controller
@RequestMapping("/clazz")
public class ClazzController extends BaseController {

    private String PREFIX = "/edu/clazz/";

    @Resource
    SClazzMapper clazzMapper;

    @Resource
    ClazzDao clazzDao;

    @Resource
    IClazzService clazzService;

    /**
     * 跳转到班级首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "clazz.html";
    }

    /**
     * 跳转到添加班级
     */
    @RequestMapping("/clazz_add")
    public String clazzAdd() {
        return PREFIX + "clazz_add.html";
    }

    /**
     * 跳转到修改班级
     */
    @RequestMapping("/clazz_update/{clazzId}")
    public String clazzUpdate(@PathVariable Integer clazzId, Model model) {
        SClazz clazz = this.clazzMapper.selectById(clazzId);
        model.addAttribute("clazz",clazz);
        return PREFIX + "clazz_edit.html";
    }

    /**
     * 获取班级的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.clazzDao.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 获取班级列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String key) {
        return clazzDao.list(key);
    }

    /**
     * 新增班级
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SClazz clazz) {
        if(ToolUtil.isOneEmpty(clazz,clazz.getName(),clazz.getCode())){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        clazz.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 删除班级
     */
    @RequestMapping(value = "/delete")
    @Permission
    @ResponseBody
    public Object delete(Integer clazzId) {
        return this.clazzMapper.deleteById(clazzId);
    }


    /**
     * 修改班级
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SClazz clazz) {
        if(ToolUtil.isOneEmpty(clazz,clazz.getName(),clazz.getCode())){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.clazzMapper.updateById(clazz);
        return super.SUCCESS_TIP;
    }

    /**
     * 班级详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(Integer clazzId) {
        return this.clazzMapper.selectById(clazzId);
    }
}
